package com.doeis.uwork.dao;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.doeis.uwork.entity.ItemNotes;
import com.google.inject.Singleton;
import com.voland.core.db.Dao;
import com.voland.core.db.DaoException;
import com.voland.core.db.Page;

@Singleton
public class ItemNotesDao extends Dao {

	public ItemNotes findItemNotesByid(Object id) throws DaoException{
		try{
			return super.readById(ItemNotes.class, id);
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}

	public Page<ItemNotes> listItemNotess(Page page) throws DaoException{
		try{
			 String sql = "select * from item_notes";
			return super.querySlice(ItemNotes.class, sql, page);
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	public ItemNotes findItemNotes(ItemNotes note) throws DaoException{
		try{
			StringBuffer sql =new StringBuffer( "select * from item_notes where 1 = 1");
			 List<Object> list = new ArrayList<Object>();
			 if(note.getItem_id()!=null){
				 sql.append(" and item_id = ? ");
				 list.add(note.getItem_id());
			 }
			 if(note.getUser_id()!=null){
				 sql.append(" and user_id = ? ");
				 list.add(note.getUser_id());
			 }
			 if(note.getType()!=null){
				 sql.append(" and type = ? ");
				 list.add(note.getType());
			 }
			 sql.append(" order by creater_at desc ");
			return super.read(ItemNotes.class, sql.toString(),list.toArray());
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	public Page<ItemNotes> listItemNotess(Page page,ItemNotes note) throws DaoException{
		try{
			 StringBuffer sql =new StringBuffer( "select * from item_notes where 1 = 1");
			 List<Object> list = new ArrayList<Object>();
			 if(note.getItem_id()!=null){
				 sql.append(" and item_id = ? ");
				 list.add(note.getItem_id());
			 }
			 if(note.getUser_id()!=null){
				 sql.append(" and user_id = ? ");
				 list.add(note.getUser_id());
			 }
			 if(note.getType()!=null){
				 sql.append(" and type = ? ");
				 list.add(note.getType());
			 }
			 sql.append(" order by creater_at desc ");
			return super.querySlice(ItemNotes.class, sql.toString(), page,list.toArray());
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}

	
	public List<ItemNotes> listItemNotess(ItemNotes note,Timestamp start,Timestamp end) throws DaoException{
		try{
			 StringBuffer sql =new StringBuffer( "select * from item_notes ");
			 List<Object> list = new ArrayList<Object>();
			 
			 sql.append(" where  creater_at >= ? ");
			 list.add(start);
			 
			 sql.append(" and  creater_at <= ? ");
			 list.add(end);
			 
			 
			 if(note.getItem_id()!=null){
				 sql.append(" and item_id = ? ");
				 list.add(note.getItem_id());
			 }
			 if(note.getUser_id()!=null){
				 sql.append(" and user_id = ? ");
				 list.add(note.getUser_id());
			 }
			 
			 sql.append(" order by creater_at desc ");
			return super.query(ItemNotes.class, sql.toString(),list.toArray());
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}
}